/**
 * Created by JetBrains WebStorm.
 * User: Mr.Tan
 * Date: 3/18/12
 * Time: 12:41 PM
 */
function jewelryControlPanel(){
    this.flags = {
        cpScript: 'index.html',
        itemName: 'dashboard',
        curItem: ''
    };
    this.lang = {
        "pleaseWait": 'Đang tải...'
    };
    this.init = function()
    {
        $(document).ready(function(){
            var locationHash = document.location.hash;
            if(locationHash=='#')
                window.location.href='index.html';
            if(locationHash.length>0)
                jewelryCP.flags['itemName'] = locationHash.substr(1);

            $('#main-nav a').each(function(){
                var ln = $(this).attr('href');
                if((ln.charAt(0) == '#') && (ln != '#'))
                {
                    $(this).click(function(){
                        window.location.href=(this.href);
                        jewelryCP.loadItem(ln.substring(1), this);
                        return false;
                    });
                }
            });

            jewelryCP.loadItem('', locationHash ? $('a[href='+locationHash+']') : '');
        });
    };
    this.cText = function (title)
    {
        return (typeof title == 'undefined') ? '' : title;
    };

    this.changeTitle = function (title)
    {
        $('#result h2').html(this.cText(title ? title : this.oldtitle));
        if(title && title!=this.lang['pleaseWait'])
			this.oldtitle = title;
    };
    this.loading = function ()
    {
        this.changeTitle(this.lang['pleaseWait']);
        $('#result').fadeTo('normal', 0.5);
    };
    this.loadItem = function (iName, menuEntry)
    {
        if(typeof menuEntry != 'undefined')
        {
            $('a').removeClass('current');
            $(menuEntry).addClass('current');
            if(!$(menuEntry).hasClass('nav-top-item'))
                $(menuEntry).parent().parent().parent().find('.nav-top-item').addClass('current');
        }
        this.loading();
        if(iName)
            this.flags['itemName'] = iName;
        $.get('html/' + this.flags['itemName'] + '.html',
            this.loadHTML,
            'html'
        );
        this.flags['curItem'] = this.flags['itemName'];

        return false;
    };
    this.loadHTML = function(data){
        $('#result').fadeTo('normal', 1.0);
        $("#result").html(data);
    };
    this.init();
}

jewelryCP = new jewelryControlPanel();